package com.tico.translatereditor.controller;

import com.alibaba.fastjson.JSONObject;
import com.tico.common.pojo.Result;
import com.tico.common.pojo.ResultHelper;
import com.tico.common.utils.BaseQuery;
import com.tico.common.utils.JsonUtils;
import com.tico.common.utils.QueryResult;
import com.tico.translatereditor.domain.KeyValuePair;
import com.tico.translatereditor.param.KeyValuePairParam;
import com.tico.translatereditor.service.KeyValuePairService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@RestController
@RequestMapping("/KeyValuePair")
@Slf4j
public class KeyValuePairController {
    @Autowired
    private KeyValuePairService keyValuePairService;

    @PostMapping(value = "/save")
    public JSONObject test(@RequestBody KeyValuePair keyValuePair, HttpServletRequest request) {
        Result res = new Result();
        try {
            keyValuePair.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            keyValuePairService.insert(keyValuePair);
            res.setMessage("成功");
            res.setSuccess(true);
        }catch (Exception e){
            e.printStackTrace();
            res.setMessage("失败");
            res.setSuccess(false);
        }
        return ResultHelper.renderAsJson(res);
    }
    @GetMapping(value = "/list")
    public JSONObject list(KeyValuePairParam keyValuePairParam) {
        Result result = new Result();
        try {
            BaseQuery baseQuery = new BaseQuery();
            baseQuery.setPage(keyValuePairParam.getPage());
            baseQuery.setLimit(keyValuePairParam.getLimit());
            QueryResult<KeyValuePair> pageInfo = keyValuePairService.findPageInfo(baseQuery);
            result.addModel("count",pageInfo.getTotalRecord());
            result.addModel("data", JsonUtils.toUnderlineJSONFilterString(KeyValuePair.class,pageInfo.getResultList(),new String[]{}) );
            result.setMessage("成功");
            result.setSuccess(true);
        }catch (Exception e){
            result.setMessage("失败");
            result.setSuccess(false);
        }
        return ResultHelper.renderAsJsonWipeData(result);
    }
    @PostMapping(value = "/delete")
    public JSONObject delete(@RequestBody KeyValuePairParam keyValuePairParam, HttpServletRequest request) {
        Result res = new Result();
        try {
            String str[] = keyValuePairParam.getIds().split(",");
            for(int i=0;i<str.length;i++){
                keyValuePairService.delete(str[i]);
            }
            res.setMessage("成功");
            res.setSuccess(true);
        }catch (Exception e){
            res.setMessage("失败");
            res.setSuccess(false);
        }
        return ResultHelper.renderAsJson(res);
    }

    @PostMapping(value = "/startUsing")
    public JSONObject startUsing(@RequestBody KeyValuePair keyValuePair, HttpServletRequest request) {
        Result res = new Result();
        try {
            String str[] = keyValuePair.getIds().split(",");
            for(int i=0;i<str.length;i++){
                keyValuePair.setId(str[i]);
                keyValuePairService.update(keyValuePair);
            }
            res.setMessage("成功");
            res.setSuccess(true);
        }catch (Exception e){
            res.setMessage("失败");
            res.setSuccess(false);
        }
        return ResultHelper.renderAsJson(res);
    }
}
